// pages/leaveMessage/leaveMessage.js
import approve from 'approvejs';
import rules from '../../utils/verifyData';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showContent: false,
    showStanding: false,
    // 表单信息
    formData: {
      linkProjectId: "",
      linkProjectName: "",
      linkFromName: "",
      linkFromIdentityTypeValue: "",
      linkFromIdentityTypeLabel: "",
      linkFromIdTypeCompanyName: "",
      linkFromMobile: "",
      linkFromMoney: "",
      linkFromRemark: "",
    },
    identityItem: {},
    temp_projectUserTypeLabel: '',
    temp_projectUserTypeValue: '',
  },
  fromVerify(verifyData) {
    var newRules = rules.LeaveMessage;
    const formData = verifyData;
    for (let i in newRules) { 
      let results = approve.value(formData[i], newRules[i]);
      if (!results.approved) {
        wx.showToast({
          title: results.errors[0],
          icon: "none"
        })
        return results.approved;
      } else {
        return true;
      }
    }
  },
  async submitFormData() {
    var {
      formData
    } = this.data; 
    if (this.fromVerify(formData)) {
      let res = await getApp().http.requestLink(formData);
      if (res.errorCode === 200) {
        wx.showModal({
          content: '已留言成功，请等待意向方联系。',
          showCancel: false,
          success() {
            wx.navigateBack();
          }
        })
      } else {
        wx.showModal({
          title: "留言失败",
          content: res.errorMessage,
          showCancel: false,
        })
      }
    }
  },
  setInput(e) {
    this.setData({
      [e.currentTarget.id]: e.detail
    })
  },
  setUserIdentitItem(e) { 
    const {
      temp_projectUserTypeLabel,
      temp_projectUserTypeValue,
      identityItem
    } = this.data;
    this.setData({
      "formData.linkFromName": identityItem.identityRealName,
      'formData.linkFromIdentityTypeLabel': temp_projectUserTypeLabel,
      'formData.linkFromIdentityTypeValue': temp_projectUserTypeValue,
      'formData.linkFromIdTypeCompanyName': identityItem.identityCompanyName,
      showStanding: false,
      showIdentity: false,
    })
  },
  setUserIdentit(e) {
    let {
      item
    } = getApp().utils.getParameter(e);
    this.setData({
      temp_projectUserTypeLabel: item.identityTypeLabel,
      temp_projectUserTypeValue: item.identityTypeValue,
      identityItem: item,
    });
  },
  closePopup() { 
    let keyArray = ['showContent', 'showStanding'];
    keyArray.forEach(item => {
      this.setData({
        [item]: false,
      })
    })
  },
  openPopup(e) {
    var key = e.currentTarget.id;
    this.setData({
      [key]: true,
    })
  },
  // 获取已认证企业身份信息
  async getIdentityList() {
    let res = await getApp().http.selectUserIdentity();
    if (res.errorCode === 200) { 
      this.setData({ 
        // identityList:res.data.filter(item=>item.identityStatusValue===1),
        identityList:res.data,
      })
    }
  },
  goAuthPage() {
    wx.navigateTo({
      url: '/pages/my/authentication/authentication',
    })
  },
  init() {
    var that = this;
    wx.getStorage({
      key: "currentUser",
      success(res) {
        that.setData({ 
          "formData.linkFromMobile": res.data.userMobile || "",
          // "formData.linkFromName": res.data.identityRealName || "",
          // "formData.linkFromIdentityTypeLabel": res.data.identityTypeLabel || "",
          // "formData.linkFromIdentityTypeValue": res.data.identityTypeValue || "",
          // "formData.linkFromIdTypeCompanyName": res.data.identityCompanyName || "", 
        })
      }
    })
    wx.getStorage({
      key: "detailsItem",
      success(res) {
        that.setData({
          "formData.linkProjectId": res.data.projectId,
          "formData.linkProjectName": res.data.projectShortName,
          content: res.data,
          showContent: true,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getIdentityList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})